package vn.com.unit.springmvc.mvc;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import vn.com.unit.springmvc.domain.AssetType;
import vn.com.unit.springmvc.repo.AssetTypeDao;

@Controller
@RequestMapping(value = "/assettype")
public class AssetTypeController {
	@Autowired
	private AssetTypeDao assetTypeDao;

	@RequestMapping(method = RequestMethod.GET)
	public String displaySortedAssetTypes(Model model) {
		model.addAttribute("newAssetType", new AssetType());
		model.addAttribute("assetTypes", assetTypeDao.findAllOrderedByName());
		return "assettype";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String registerNewAssetType(
			@Valid @ModelAttribute("newAssetType") AssetType newAssetType,
			BindingResult result, Model model) {
		if (!result.hasErrors()) {
			// AssetType obj = assetTypeDao.findByCode(newAssetType.getCode());
			// if (obj == null) {
			assetTypeDao.register(newAssetType);
			// }
			return "redirect:/assettype";
		} else {
			model.addAttribute("assetTypes",
					assetTypeDao.findAllOrderedByName());
			return "assettype";
		}

	}

	@RequestMapping(value = "/search", method = RequestMethod.POST)
	public String search(@RequestParam String name, Model model) {
		model.addAttribute("assetTypes", assetTypeDao.findByName(name));
		return "assettype";
	}

	@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
	public String viewAssetTypeById(@PathVariable("id") Long id, Model model) {
		AssetType obj = assetTypeDao.findById(id);
		model.addAttribute("assetType", obj);
		return "view_assettype";
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String editAssetTypeById(@PathVariable("id") Long id, Model model) {
		AssetType obj = assetTypeDao.findById(id);
		model.addAttribute("editAssetType", obj);
		model.addAttribute("isEdit", true);
		return "view_assettype";
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
	public String deleteAssetTypeById(@PathVariable("id") Long id, Model model) {
		assetTypeDao.delete(id);
		// model.addAttribute("newAssetType", new AssetType());
		// model.addAttribute("assetTypes",
		// assetTypeDao.findAllOrderedByName());
		// return "assettype";
		return "redirect:/assettype";
	}

	// @RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	// public String deleteAssetTypeById(@PathVariable("id") String id, Model
	// model)
	// {
	// assetTypeDao.delete(id);
	// model.addAttribute("newAssetType", new AssetType());
	// model.addAttribute("assetTypes", assetTypeDao.findAllOrderedByName());
	// return "assettype";
	// }

	// @RequestMapping(value="/delete", method=RequestMethod.GET)
	// public String deleteAssetTypeById(@RequestParam String id)
	// {
	// assetTypeDao.delete(id);
	// //model.addAttribute("assetTypes", assetTypeDao.findAllOrderedByName());
	// //return "index";
	// return "redirect:/";
	// }
}
